package cn.qn.flowable.listener;

import lombok.RequiredArgsConstructor;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.flowable.engine.delegate.event.FlowableCancelledEvent;
import org.flowable.engine.delegate.event.FlowableProcessStartedEvent;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.variable.api.event.FlowableVariableEvent;
import org.springframework.stereotype.Component;

/**
 * 主要用于监听流程引擎中的全局事件，如流程实例的启动、结束，任务的创建、分配、完成等。
 *
 * @author qn
 * @date 2025/06/22
 */
@Component
@RequiredArgsConstructor
public class GlobalAuditListener extends AbstractFlowableEngineEventListener {

    /**
     * 流程创建
     *
     * @param event 事件
     */
    @Override
    protected void processCreated(FlowableEngineEntityEvent event) {
        ProcessInstance entity = (ProcessInstance) event.getEntity();

    }

    /**
     * 流程完成
     *
     * @param event 事件
     */
    @Override
    protected void processCompleted(FlowableEngineEntityEvent event) {
        ProcessInstance entity = (ProcessInstance) event.getEntity();
    }

    /**
     * 流程已取消
     *
     * @param event 事件
     */
    @Override // 特殊情况：当跳转到 EndEvent 流程实例未结束, 会执行 deleteProcessInstance 方法
    protected void processCancelled(FlowableCancelledEvent event) {
        String processInstanceId = event.getProcessInstanceId();
    }

    @Override
    protected void taskCreated(FlowableEngineEntityEvent event) {
        System.out.println("全局监听：GlobalAuditListener-任务创建");
    }
    @Override
    protected void variableCreated(FlowableVariableEvent event) {
        System.out.println("全局监听：GlobalAuditListener-创建变量");
    }
    @Override
    protected void processStarted(FlowableProcessStartedEvent event) {
        System.out.println("全局监听：GlobalAuditListener-开始流程");
    }
    @Override
    protected void taskAssigned(FlowableEngineEntityEvent event) {
        System.out.println("全局监听：任务分配");
    }
}